package com.yuwenchao.superafflatus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuwenchao.superafflatus.mapper.SuperDevProgressMapper;
import com.yuwenchao.superafflatus.pojo.entity.SuperDevProgress;
import com.yuwenchao.superafflatus.service.SuperDevProgressService;
import com.yuwenchao.superafflatus.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ChaooGG
 * @since 2021-08-03
 */
@Api(tags = "开发日志展示")
@Slf4j
@RestController
@RequestMapping("/superDevProgress")
public class SuperDevProgressController {

    @Autowired
    RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    SuperDevProgressService superDevProgressService;

    @Resource
    SuperDevProgressMapper progressMapper;

    //首页展示
    @ApiOperation("开发日志展示列表")
    @GetMapping("getDevProInfo")
    //@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    public Page<SuperDevProgress> getDevProInfo(@RequestParam(value="page",defaultValue="1") Integer page){

        /**
         * 这里由于是动态取值,不建议存入redis,否则会导致分页失效
         */
        Page<SuperDevProgress> pageParam = new Page<>(page,3);
        QueryWrapper<SuperDevProgress> queryWrapper = new QueryWrapper<SuperDevProgress>().orderByDesc("create_time");
        Page<SuperDevProgress> infoPage = superDevProgressService.page(pageParam,queryWrapper);
      //  List<SuperDevProgress> list = infoPage.getRecords();//获取数据集合
      //  redisTemplate.opsForValue().set("infoPageWrapper",queryWrapper);
        return infoPage;
    }


    //列表查询展示
/*    @ApiOperation("查询开发日志展示列表")
    @GetMapping("getDevProList")
    public R getTablePage(@RequestParam(value="page",defaultValue="1") Integer page,
                          @RequestParam(value="remark",defaultValue="") String remark){

        Page<SuperDevProgress> pageParam = new Page<>(page, AfflatusConstant.LIMIT_SIZE);
        QueryWrapper<SuperDevProgress> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("progress",remark).or().like("problem",remark);

        Page<SuperDevProgress> superDevProgressPage = progressMapper.selectPage(pageParam, queryWrapper);


        return R.ok().data("superDevProgressPage",superDevProgressPage);

    }*/



    @ApiOperation("开发日志数据表格")
    @GetMapping("getDevProListSearch")
    public Map getLayuiSearch(@RequestParam(value="page",defaultValue="1") Integer page,
                              @RequestParam(value="limit",defaultValue="10") Integer limit){
        Page<SuperDevProgress> pageParam = new Page<>(page,limit);
        QueryWrapper<SuperDevProgress> queryWrapper = new QueryWrapper<SuperDevProgress>().orderByDesc("create_time");
        Page<SuperDevProgress> infoPage = superDevProgressService.page(pageParam,queryWrapper);
        HashMap<String,Object> map = new HashMap<>();
        map.put("count", infoPage.getTotal());
        map.put("data", infoPage.getRecords());
        map.put("code", 0);
        map.put("msg", "");
        return map;
    }



    @ApiOperation("数据表格搜索查询")
    @PostMapping("/queryDevByOther")
    public Map queryUserByOther(@RequestParam(value="page",defaultValue="1") Integer page,
                                @RequestParam(value="limit",defaultValue="10") Integer limit,
                                @RequestParam(value="key",required = false) String key){
        Page<SuperDevProgress> pageParam = new Page<>(page,limit);
        QueryWrapper<SuperDevProgress> devProgressQueryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            devProgressQueryWrapper.like("progress",key).or().like("problem",key);
        }
        Page<SuperDevProgress> infoPage = superDevProgressService.page(pageParam,devProgressQueryWrapper);
        HashMap<String,Object> map = new HashMap<>();
        map.put("count", infoPage.getTotal());
        map.put("data", infoPage.getRecords());
        map.put("code", 0);
        map.put("msg", "");
        return map;
    }


    @ApiOperation("新增开发日志")
    @PostMapping("saveDevProgress")
    public  R saveDevProgress(@RequestBody SuperDevProgress devProgress){
        progressMapper.insert(devProgress);
        return R.ok();
    }

    //修改日志文件
    @ApiOperation("根据id修改开发日志列表")
    @PostMapping("updateDevProgress/{id}")
    public R updateDevProInfo(@PathVariable Integer id, @RequestBody SuperDevProgress devProgress){
        progressMapper.updateById(devProgress);
        return  R.ok();
    }
    //单个删除日志文件
    @ApiOperation("根据id删除开发日志列表")
    @DeleteMapping("delDevProgress/{id}")
    public R delDevProInfo(@PathVariable Integer id) {
        progressMapper.deleteById(id);
        return R.ok();
    }


    @ApiOperation("批量删除日志")
    @DeleteMapping("delDevProgressAll")
    public R delDevProgressAll(@RequestParam("ids") String ids) {
        //System.out.println("获取到的ids"+ids);
        if(ids.contains(",")) {
            List<Integer> list = new ArrayList<>();
            String[] split =ids.trim().split(",");
            for (String string : split) {
                list.add(Integer.parseInt(string));
            }
            System.out.println(list);
            progressMapper.deleteBatchIds(list);
        }else {
            Integer id = Integer.parseInt(ids);
            progressMapper.deleteById(id);
        }
        return R.ok();
    }




}

